package com.paly.client.configer;

import com.paly.client.api.IUserApi;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import proxy.ProxyCreator;
import proxy.impl.jdkProxyCreator;

@Component
public class UserBeanFactory {

    @Bean
    FactoryBean<ProxyCreator> proxyCreator() {
        return new FactoryBean<>() {
            @Override
            public ProxyCreator getObject() throws Exception {
                return new jdkProxyCreator();
            }

            @Override
            public Class<?> getObjectType() {
                return ProxyCreator.class;
            }
        };
    }

    @Bean
    FactoryBean<IUserApi> userApi(ProxyCreator proxyCreator) {
        return new FactoryBean<>() {
            @Override
            public IUserApi getObject() throws Exception {
                return (IUserApi) proxyCreator.createProxy(this.getObjectType());
            }

            @Override
            public Class<?> getObjectType() {
                return IUserApi.class;
            }
        };
    }
}
